package com.nondo.dean.platform.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.platform.domain.Freight;
import com.nondo.dean.platform.domain.FreightExample;
import com.nondo.dean.platform.domain.Zone;
import com.nondo.dean.platform.mapper.FreightMapper;
import com.nondo.dean.platform.mapper.ZoneMapper;
import com.nondo.dean.platform.model.result.FreightResult;

/**
 * 区域运费折扣管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class FreightService {

    @Autowired
    private FreightMapper freightMapper;
    
    @Autowired
    private ZoneMapper zoneMapper;

    /**
     * 插入区域运费折扣
     * @param freight
     * @return
     */
    @Transactional
    public int addFreight(Freight freight) {
        return freightMapper.insertSelective(freight);
    }

    /**
     * 修改区域运费折扣
     * @param freight
     * @return
     */
    @Transactional
    public int updateFreight(Freight freight) {
        return freightMapper.updateByPrimaryKeySelective(freight);
    }

    /**
     * 按照主键ID查询区域运费折扣
     * @param id
     * @return
     */
    public Freight selectFreight(Integer id) {
        return freightMapper.selectByPrimaryKey(id);
    }

    /**
     * 区域运费折扣分页查询列表
     * @param offset
     * @param limit
     * @param freight
     * @return
     */
    public List<Freight> getFreightList(int offset, int limit, Freight freight) {
        FreightExample example = new FreightExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        FreightExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, freight);
        return freightMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 区域运费折扣总数统计
     * @param freight
     * @return
     */
    public Long count(Freight freight) {
        FreightExample example = new FreightExample();
        FreightExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, freight);
        return freightMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param freight
     */
    void setCriteria(FreightExample.Criteria criteria, Freight freight) {
        if (freight != null) {
            if (freight.getId() != null)
                criteria.andIdEqualTo(freight.getId());
            if (freight.getZoneId() != null)
                criteria.andZoneIdEqualTo(freight.getZoneId());
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

    /**
     * 根据区域ID获取区域运费折扣
     * @param zoneId 区域ID
     * @return 指定区域运费折扣 {@link Freight}
     */
    public Freight getFreightByZoneId(Integer zoneId) {
        FreightExample example = new FreightExample();
        FreightExample.Criteria criteria = example.createCriteria();
        criteria.andZoneIdEqualTo(zoneId);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Freight> freightList = freightMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(freightList)?freightList.get(0):null;
    }

    /**
     * 区域运费折扣详情
     * @param freightDb {@link Freight}
     * @return {@link FreightResult}
     */
    public FreightResult getFreightDetail(Freight freightDb) {
        FreightResult freightResult = FreightResult.covertToBean(freightDb);
        Zone zone = zoneMapper.selectByPrimaryKey(freightDb.getZoneId());
        freightResult.setZoneName(zone.getName());
        return freightResult;
    }

}
